package com.ce.pms.house.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.house.dao.IReasonDefDao;
import com.ce.pms.house.model.GoodsRecordDomain;
import com.ce.pms.house.model.ReasonDefDomain;

public class ReasonDefDaoImpl extends PmsBaseDaoImpl<ReasonDefDomain> implements IReasonDefDao{

	public boolean isUsed(ReasonDefDomain reason) {
		StringBuffer sql = new StringBuffer("SELECT * FROM HM_GO_OUT WHERE HM_GO_OUT.REASON_DEF_ID1 = ");
		sql.append(reason.getKeyword())
		   .append(" OR HM_GO_OUT.REASON_DEF_ID2 = ")
		   .append(reason.getKeyword())
		   .append(" OR HM_GO_OUT.REASON_DEF_ID3 = ")
		   .append(reason.getKeyword());

		SQLQuery query = (SQLQuery) super.createSQLQuery(sql.toString(), new Object[]{});

		return (query.list().size() > 0)?true:false;
	}

	public boolean nameAlreadyExist(ReasonDefDomain reason) {
		StringBuffer sql = new StringBuffer("SELECT * FROM HM_REASON_DEF WHERE HM_REASON_DEF.NAME = ? ");
		List<Object> parameter = new ArrayList<Object>();
		parameter.add(reason.getName());
		//在指定的类型中比较
		if(reason.getType() != null)
		{
			SysrefcodeDO sysRef = reason.getType();
			sql.append(" AND HM_REASON_DEF.SYS_REF_ID= ? ");
			parameter.add(sysRef.getId());
		}
		//如果是查询出来的则不比较自身
		if (reason.getKeyword() != null ) {
			sql.append(" AND HM_REASON_DEF.REASON_DEF_ID <> ?");
			parameter.add(reason.getKeyword());
		}

		SQLQuery query = (SQLQuery) super.createSQLQuery(sql.toString(),parameter.toArray());

		return (query.list().size() > 0)?true:false;
	}

	public boolean alreadyExist(ReasonDefDomain reason)
	{
		StringBuffer sql = new StringBuffer("SELECT * FROM HM_REASON_DEF WHERE HM_REASON_DEF.NAME = ? ");
		List<Object> parameter = new ArrayList<Object>();
		parameter.add(reason.getName());

		//在指定的类型中比较
		if(reason.getType() != null)
		{
			SysrefcodeDO sysRef = reason.getType();
			sql.append(" AND HM_REASON_DEF.SYS_REF_ID= ? ");
			parameter.add(sysRef.getId());
		}
		//如果是查询出来的则不比较自身
		if (reason.getKeyword() != null ) {
			sql.append(" AND HM_REASON_DEF.REASON_DEF_ID <> ?");
			parameter.add(reason.getKeyword());
		}

		SQLQuery query = (SQLQuery) super.createSQLQuery(sql.toString(), parameter.toArray());

		return (query.list().size() > 0)?true:false;
	}

	/**
	 * 方法描述: 根据异常类型查询相应异常集合
	 * @author 胡煦
	 * Create on:2008-9-4
	 * @param reasonDefType
	 * @return List<ReasonDefDomain>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<ReasonDefDomain> getReasonDefDoList(String reasonDefType){
		String queryStr = "SELECT * FROM `HM_REASON_DEF` hgr JOIN SYS_REF_CODE src ON hgr.SYS_REF_ID = src.SYS_REF_ID WHERE src.SYS_REF_VALUE = ? ORDER BY IF(hgr.`SEQ_NO` is NULL,(SELECT max(hgr1.`SEQ_NO`) FROM `HM_REASON_DEF` hgr1)+1,hgr.`SEQ_NO`);";

		Query query=this.createSQLQuery(queryStr,reasonDefType);
		List<Object[]> listObj = query.list();
		List<ReasonDefDomain> list = new ArrayList<ReasonDefDomain>();
		for(Object[] objects : listObj){
			ReasonDefDomain reasonDefDomain = new ReasonDefDomain();
			reasonDefDomain.setKeyword(new Long(objects[0].toString()));
			reasonDefDomain.setName(objects[1].toString());
			reasonDefDomain.setDescription(objects[2]==null?null:objects[2].toString());
			reasonDefDomain.setSeqNo((Short) objects[4]);
			list.add(reasonDefDomain);
		}
		return list;
	}

}
